package top.lgd.service.study;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.lgd.enums.DateEnum;
import top.lgd.mapper.study.RecordMapper;
import top.lgd.pojo.entity.study.Record;
import top.lgd.pojo.res.study.CatalogDurationRes;
import top.lgd.pojo.res.study.DayDurationRes;
import top.lgd.utils.DateUtils;
import top.lgd.utils.NumberUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 学习内容表 服务实现类
 *
 * @author YYJ
 * @version 1.0
 * @since 2023/12/15 10:10
 */
@Service
public class RecordService extends ServiceImpl<RecordMapper, Record> implements IService<Record> {

    private int days = 29;

    /**
     * @return
     */
    public List<DayDurationRes> queryDurationList(Long userId) {

        LocalDate localDate = LocalDate.now();
        List<DayDurationRes> tmpList = baseMapper.queryDurationList(userId, DateUtils.toDateString(localDate.minus(days, ChronoUnit.DAYS)));
        Map<String, DayDurationRes> tmpMap = tmpList.stream().collect(Collectors.toMap(DayDurationRes::getStudyDay, v -> v));

        List<DayDurationRes> durationResList = new ArrayList<>(32);

        for (int i = days; i >= 0; i--) {
            LocalDate tmpDate = localDate.minus(i, ChronoUnit.DAYS);
            DayDurationRes dayDurationRes = tmpMap.get(DateUtils.toDateString(tmpDate));
            if (null != dayDurationRes) {
                durationResList.add(new DayDurationRes(DateUtils.toShortDate(tmpDate), dayDurationRes.getDuration(), dayDurationRes.getCurrentIndex()));
            } else {
                durationResList.add(new DayDurationRes(DateUtils.toShortDate(tmpDate), 0, 0));
            }
        }
        return durationResList;
    }

    /**
     * @param userId
     * @return
     */
    public List<CatalogDurationRes> catalogDurationList(Long userId) {
        return baseMapper.catalogDurationList(userId);
    }

    /**
     * 返回小时
     *
     * @return
     */
    public BigDecimal sumDuration(Long userId, DateEnum type) {
        LocalDate localDate = LocalDate.now();
        String startDate;
        String endDate;
        switch (type) {
            case YD:
                startDate = DateUtils.toDateString(localDate.minus(1, ChronoUnit.DAYS));
                endDate = DateUtils.toDateString(localDate);
                break;
            case TW:
                startDate = DateUtils.toDateString(localDate.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY)));
                endDate = DateUtils.toDateString(localDate.plus(1, ChronoUnit.DAYS));
                break;
            case LW:
                startDate = DateUtils.toDateString(localDate.minusWeeks(1).with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY)));
                endDate = DateUtils.toDateString(localDate.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY)));
                break;
            case TM:
                startDate = DateUtils.toDateString(localDate.with(localDate.with(TemporalAdjusters.firstDayOfMonth())));
                endDate = DateUtils.toDateString(localDate.plus(1, ChronoUnit.DAYS));
                break;
            case LM:
                startDate = DateUtils.toDateString(localDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()));
                endDate = DateUtils.toDateString(localDate.with(localDate.with(TemporalAdjusters.firstDayOfMonth())));
                break;
            case TY:
                startDate = DateUtils.toDateString(localDate.with(TemporalAdjusters.firstDayOfYear()));
                endDate = DateUtils.toDateString(localDate.plus(1, ChronoUnit.DAYS));
                break;
            case LY:
                startDate = DateUtils.toDateString(localDate.minusYears(1).with(TemporalAdjusters.firstDayOfYear()));
                endDate = DateUtils.toDateString(localDate.with(TemporalAdjusters.firstDayOfYear()));
                break;
            default:
                startDate = DateUtils.toDateString(localDate);
                endDate = DateUtils.toDateString(localDate.plus(1, ChronoUnit.DAYS));
                break;
        }
        Integer duration = baseMapper.sumDuration(userId, startDate, endDate);
        return NumberUtils.div(NumberUtil.toBigDecimal(duration), BigDecimal.valueOf(60), 1);
    }
}
